const { sequelize } = require('../utils/sequelize');
const { v4: uuidv4 } = require('uuid');

class TeacherService {
    constructor() {
    }

    async findAll(pageSize, pageNum){
        let data = null;

        let offset = pageSize * (pageNum - 1);
        try {
            data =  await sequelize.models.Teacher.findAndCountAll({
                offset,
                limit: pageSize * 1
            });
        } catch (error) {
            console.log(error);
            error['name'] = error.name || '未知错误'
            return error;
        }
        return data;
    }

    async getOneAttr(id, attr) {
        let attrValue = '';
        let cateory = await sequelize.models.Teacher.findOne({
            attributes: [ attr ],
            where: {userId: id}
        });
        attrValue = cateory.getDataValue(attr)
        return attrValue;
    }

    async getAttrs(id, attr) {
        let attrValue = '';
        let cateory = await sequelize.models.Teacher.findOne({
            attributes: attr,
            where: {userId: id}
        });
        // attrValue = cateory.getDataValue(attr)
        return cateory;
    }

    async findAttrById(userId){
        let data = null;
        data = await sequelize.models.Teacher.findOne({
            where: {userId: userId}}
        );
        
        return data;
    }

    async saveTeacher(user){
        let data = null;
        // console.log(user)
        user['userId'] = uuidv4();
        user['headUrl'] = "http://139.199.69.126:10000/static/head.gif";
        data = await sequelize.models.Teacher.create(user)
        return data;
    }

    async deleteTeacher(userId){
        let data = null;
        data = await sequelize.models.Teacher.destroy({where: {user_id: userId}});
        return data;
    }

    async updateTeacher(user){
        let data = null;
        let attr = [ 'nickname', 'headUrl', 'sex', 'teacherId', 'telephone', 'identity']
        let userObj = {};
        attr.forEach(function(e){
            if(user[e] != undefined){
                userObj[e] = user[e]
            }
        })
        
        data = await sequelize.models.Teacher.update({...userObj}, {
            where: {userId: user.userId}
        })
        return data;
    }

    async updatePassword(user) {
        let data = null;
            let oldObj = await sequelize.models.Teacher.findOne({
                attributes: ['password'],
                where: {
                    userId: user.userId
                }
            })
            if(oldObj.getDataValue('password') != user.oldPassword) return {name: '旧密码不正确'}
            data = await sequelize.models.Teacher.update({password: user.newPassword}, {
                where: {userId: user.userId}
            })
        return data
    }

    async login(user){
        let data = null;
        let attr = [ 'userId', 'nickname', 'username', 'headUrl', 'sex', 'telephone', 'identity']
            data = await sequelize.models.Teacher.findOne({
                where: {
                    username: user.username
                }
            })
        
        if(!data) return 210
        if(data.password != user.password) return 211
        let userObj = {};
        attr.forEach(function(e){
            userObj[e] = data[e]
        })
        return userObj;
    }
}

module.exports = new TeacherService();